﻿using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Assembly
{
    public class PipeServer
    {
        private static NamedPipeServerStream pipeServer;
        private static bool runFunctionActive = false;

        public static void Start()
        {
            Thread serverThread = new Thread(RunServer);
            serverThread.Start();

            // 模拟运行 run 函数
            RunFunction();
            Console.WriteLine("Run function completed.");
        }

        private static void RunServer()
        {
            while (true)
            {
                using (pipeServer = new NamedPipeServerStream("blockPipe", PipeDirection.InOut))
                {
                    Console.WriteLine("Waiting for client connection...");
                    pipeServer.WaitForConnection();
                    Console.WriteLine("Client connected.");

                    try
                    {
                        using (StreamReader sr = new StreamReader(pipeServer))
                        using (StreamWriter sw = new StreamWriter(pipeServer) { AutoFlush = true })
                        {
                            string message = sr.ReadLine();
                            if (message == "StartRun")
                            {
                                runFunctionActive = true;
                                Console.WriteLine("Received start signal from client.");
                            }
                            else if (message == "StopRun")
                            {
                                runFunctionActive = false;
                                Console.WriteLine("Received stop signal from client.");
                            }

                            // 反馈给客户端
                            sw.WriteLine($"Acknowledged: {message}");
                        }
                    }
                    catch (IOException e)
                    {
                        Console.WriteLine($"Error: {e.Message}");
                    }
                }
            }
        }

        private static void RunFunction()
        {
            while (!runFunctionActive) { Thread.Sleep(100); } // 等待启动信号
            Console.WriteLine("Executing run function...");
            Thread.Sleep(5000); // 模拟长时间运行的任务
            Console.WriteLine("Run function is running.");
        }
    }
}

/*
 * using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace EMSharpPLCPlugIns
{
    public class Server
    {
        private static NamedPipeServerStream pipeServer;

        public static void Start()
        {
            if(pipeServer == null)
            {
                pipeServer = new NamedPipeServerStream("blockPipe", PipeDirection.InOut);
            }
            
            Thread serverThread = new Thread(RunServer);
            serverThread.Start();
        }

        private static void RunServer()
        {
            
            //while(true)
            {
                Console.WriteLine("Waiting for client connection...");
                pipeServer.WaitForConnection();
                Console.WriteLine("Client connected.");
                try
                {
                    using (StreamReader sr = new StreamReader(pipeServer))
                    using (StreamWriter sw = new StreamWriter(pipeServer) { AutoFlush = true })
                    {
                        while (pipeServer.IsConnected)
                        {
                            AppDomainDelete(sr, sw);
                        }
                    }
                }
                catch (IOException e)
                {
                    Console.WriteLine($"Error: {e.Message}");
                }
                finally
                {
                    Console.WriteLine("Client disconnected.");
                    // 准备接受下一个连接
                    pipeServer.Dispose();
                        
                    pipeServer = new NamedPipeServerStream("blockPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message);
                }
            }
        }

        private static void AppDomainDelete(StreamReader sr, StreamWriter sw)
        {
            string message = sr.ReadLine();

            if (message == "DeleteBeforeDll")
            {
                EMSharpPLCPlugAPI.UnLoadDll("DeleteBeforeDll");
                sw.WriteLine("DeleteBeforeSuccess");
            }
            else if (message == "DeleteEndSuccess")
            {
                EMSharpPLCPlugAPI.UnLoadDll("DeleteEndSuccess");
                sw.WriteLine("DeleteEndSuccess");
            }
        }
    }
}

 * 
 * /